﻿using System.Collections.Generic;
using CodeGenerator.CSharp.Extensions;

namespace CodeGenerator.CSharp
{
    public class ClassBuilder : CodeUnit
    {

        #region Constructors

        public ClassBuilder(string name) : base(name) { }
        public ClassBuilder(string name, ModifierCollection modifiers) : base(name, modifiers) { }

        #endregion

        #region BaseClass & Interfaces

        /// <summary>
        /// Identifies the base class for this class, if any.
        /// </summary>
        public string BaseClass { get; set; }
        private bool HasBaseClass
        {
            get { if (this.BaseClass.IsSomething()) return this.BaseClass.Length > 0; else return false; }
        }

        /// <summary>
        /// Identifies any interfaces that this class implements.
        /// </summary>
        public List<string> Interfaces { get { return this._Interfaces; } }
        private List<string> _Interfaces = new List<string>();
        private bool HasInterfaces { get { return this._Interfaces.Count > 0; } }
        private string InterfacesToString()
        {
            string ReturnValue = string.Empty;
            foreach (string ThisInterface in this._Interfaces)
                ReturnValue += ThisInterface + ", ";

            //Remove any extra connectors.
            if (ReturnValue.Length > 2) ReturnValue = ReturnValue.Remove(ReturnValue.Length - 2, 2);

            return ReturnValue;
        }

        #endregion

        #region ToString
        public override string ToString()
        {
            CodeBuilder Code = new CodeBuilder();

            base.AppendDocumentation(Code);
            base.AppendAttributes(Code);

        
            Code.Append("{0}{1} class {2}", base.Padding, base.Modifiers, base.Name);
            if (this.HasBaseClass && this.HasInterfaces)
                Code.Append(" : {0}, {1}", this.BaseClass, this.InterfacesToString());
            else if (this.HasBaseClass)
                Code.Append(" : " + this.BaseClass);

            else if (this.HasInterfaces)
                Code.Append(" : " + this.InterfacesToString());
            Code.Add();
            Code.Add(base.Padding + "{");
            Code.Add(this.CodeUnits);
            Code.Add(base.Padding + "}");

            return Code.ToString();
        } 
        #endregion

    }

}
